package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.advantagesup.common.PaginationVO;
import com.itheima.advantagesup.dto.UserAddDTO;
import com.itheima.advantagesup.entity.SysUser;
import com.itheima.advantagesup.mapper.SysUserMapper;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class SysUserServiceImpl implements SysUserService {
    private SysUserMapper sysUserMapper;

    @Autowired
    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public PaginationVO<UserInfoVO> page(String loginName, String status, String phone, String page, String limit) {

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<SysUser> list = sysUserMapper.list(loginName, status, phone);
        List<SysUser> listAll = new ArrayList<>();
        list.forEach(name -> {
            String roleId = name.getRoleId();
            System.out.println(roleId);
            String[] split = roleId.substring(2, roleId.length() - 2).split(",");
            List<String> namesList = new ArrayList<>();
            for (String s : split) {
                List<SysUser> sysUserList = sysUserMapper.selectByRoleId(s);
                sysUserList.forEach(sysUser -> {
                    String names = sysUser.getName();
                    namesList.add(names);
                });
            }
            name.setRolename(namesList);
            listAll.add(name);
        });
        System.out.println(listAll);

        PageInfo<SysUser> pi = new PageInfo<>(listAll);
        long count = pi.getTotal();
        int totalPages = pi.getPages();
        int pageSize = pi.getPageSize();
        int currentPage = pi.getNextPage();
        List<SysUser> sysUserList = pi.getList();

        List<UserInfoVO> userVOList = new ArrayList<>();
        sysUserList.forEach(sysUser -> {
            System.out.println(sysUser);
            userVOList.add(sysUser.getUser());
        });
        System.out.println(userVOList);
        PaginationVO<UserInfoVO> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(userVOList);
        return paginationVO;
    }

    @Override
    public void addUser(UserInfoVO user) {
        String id = UUID.randomUUID().toString();
        user.setId(id);
        UserAddDTO userAddDTO = user.getUserAddDTO();
        log.info("UserAddDTO:{}", userAddDTO);
        sysUserMapper.addUser(userAddDTO);
    }

    @Override
    public UserInfoVO rowInfoUser(String id) {
        SysUser sysUser = sysUserMapper.rowInfoUser(id);
        String roleId = sysUser.getRoleId();
        String[] split = roleId.substring(2, roleId.length() - 2).split(",");
        List<String> role_id = new ArrayList<>();
        Collections.addAll(role_id, split);
        UserInfoVO user = sysUser.getUser();
        user.setRole_id(role_id);
        return user;
    }

    @Override
    public void delUser(String id) {
        sysUserMapper.delUser(id);
    }

    @Override
    public void aupdateUser(UserInfoVO user) {
        Integer status = user.getStatus();
        System.out.println(status);
        if (status == 2) {
            status -= 2;
        }
        user.setStatus(status);
        UserAddDTO userAddDTO = user.getUserAddDTO();
        if ( user.getRole_id() != null){
            List<String> roleId = user.getRole_id();
            if (!roleId.isEmpty()) {
                StringJoiner str = new StringJoiner(",", "[\"", "\"]");
                roleId.forEach(r -> {
                    if (!r.contains("[\"")) {
                        str.add(r);
                    }
                });
                userAddDTO.setRole_id(str.toString());
            }
        }

        log.info("UserAddDTO:{}", userAddDTO);
        sysUserMapper.aupdateUser(userAddDTO);
    }
}
